<%--
  Created by IntelliJ IDEA.
  User: Lanan
  Date: 2017/12/11 0011
  Time: 20:09
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <%@ include file="/commons/basejs.jsp" %>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>单项分类</title>
    <script type="text/javascript">

    </script>
    <style>
        .datagrid-btable td{
            line-height: 18px !important;
        }
    </style>
</head>
<body>
<div class="easyui-layout" fit="true" >
    <!--center标签列表start-->
    <div data-options="region:'center',split:true,border:false" title="单项分类列表" style="width:35%;">
        <table id="single_projoct_categary_datagrid" class="easyui-datagrid"
               data-options="
		   		url:'${path}/single/project/category/list.do',
		   		fit:true,
		   		idField:'id',
		   		rownumbers:true,
		   		toolbar:'#tools',
		   		pagination:true,
		   		fitColumns:true,
		   		singleSelect:true,
		   		onClickRow:function(index, row){
		   			getSingleApply(row.id);
		   		}">
            <%--,formatter:function(update_time){return new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(update_time)}--%>
            <thead>
            <th data-options="field:'id', align:'center'">ID</th>
            <th data-options="field:'name', align:'center'">分类名称</th>
            <%--<th data-options="field:'createTime',align:'center'">创建时间</th>--%>
            <%--<th data-options="field:'updateTime',align:'center'">更新时间</th>--%>
            <th data-options="field:'ord',align:'center'">排序</th>
            <th data-options="field:'status',align:'center',formatter:formatterStatus">是否启用</th>
            </thead>
        </table>
        <div id="tools">
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" onclick="$('#single_projoct_categary_datagrid').datagrid('reload');">刷新</a>

            <shiro:hasPermission name="/single/project/categary/insert.do">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="openAddBox()">添加</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="/single/project/categary/update.do">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="openUpdateBox()">修改</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="/single/project/categary/delete.do">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="deleteSingleProjectCaregaryInfo()">删除</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="/single/project/changeCategoryStatusToEnableOrDisable.do">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-tip'" onclick="changeSingleStatusToEnableOrDisable(true)">启用|禁用</a>
            </shiro:hasPermission>

            <form action="#" id="searchForm">
                <input id="name" name="name" class="easyui-textbox" data-options="prompt:'请输入单项分类名称'" style="width:200px"></input>
                <lable>是否启用:</lable>
                <select id="search_status" class="easyui-combobox" name="status" data-options="editable:false,panelHeight:'auto'" style="width:100px">
                    <option value="">--请选择--</option>
                    <option value="0">否</option>
                    <option value="1">是</option>
                </select>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="doSearch('searchForm', 'single_projoct_categary_datagrid')">查询</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="searchReset('searchForm', 'search_status', 'single_projoct_categary_datagrid')">重置</a>
            </form>
        </div>
    </div>
    <!--end-->
    <!--edit begin-->
    <div class="easyui-dialog" id="edit_dialog" data-options="closed:true,modal:true,width:350,height:230">
        <form id="edit_form" method="post">
            <table align="center" width="100%" style="margin-top: 20px" cellpadding="4">
                <input type="hidden" name="id"/>
                <tr>
                    <td align="right" width="30%"><lable for="name">单项类名称:</lable></td>
                    <td width="70%">
                        <input id="edit_name" name="name" class="easyui-validatebox" data-options="required:true"/>
                    </td>
                </tr>
                <tr>
                    <td align="right" width="30%"><lable for="ord">排序（大前小后）:</lable></td>
                    <td width="70%">
                        <input id="edit_ord" name="ord" class="easyui-numberbox" data-options="required:true"/>
                    </td>
                </tr>
                <%--<tr>--%>
                <%--<td align="right" width="30%"><lable for="status">是否启用:</lable></td>--%>
                <%--<td width="70%">--%>
                <%--<select id="edit_status" class="easyui-combobox" name="status" data-options="required:true,panelHeight:'auto',editable:false" style="width:160px;">--%>
                <%--<option value="0">否</option>--%>
                <%--<option value="1">是</option>--%>
                <%--</select>--%>
                <%--</td>--%>
                <%--</tr>--%>
            </table>
            <div style="margin-top: 20px;text-align: center">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'", onclick="handleSingleProjectCategaryInfo()">保存</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'", onclick="$('#edit_dialog').dialog('close')">取消</a>
            </div>
        </form>
    </div>
    <!--edit end-->

    <script>

        function openAddBox(){
            flag = 'add';
            $("#edit_form").form("clear");
            $("#edit_status").combobox('setValue', 1);
            $("#edit_dialog").dialog({title: "添加单项分类"});
            $("#edit_dialog").dialog("open");
        }

        function openUpdateBox() {
            flag = 'update';
            var select = $("#single_projoct_categary_datagrid").datagrid("getSelected");
            if(select == null){
                $.messager.alert("提示","请选择要修改的单项分类！","info");
                return;
            }
            $("#edit_form").form("load", select);
            $('#edit_dialog').dialog({title: "修改单项分类"});
            $("#edit_dialog").dialog("open");
        }

        function deleteSingleProjectCaregaryInfo(){
            lananDelete("single_projoct_categary_datagrid", "确定要删除该单项分类吗?", "${path}/single/project/categary/delete.do");
        }

        function getSingleApply(singleProjectCategoryId){
            var select = $("#single_datagrid").datagrid("getSelected");
            if(select != null){
                $('#single_datagrid').datagrid('clearSelections');
            }
            searchReset('searchSingleForm', 'search_single_status', 'single_datagrid');
            var queryParams = {};
            queryParams.singleProjectCategoryId = singleProjectCategoryId;
            var options = $("#single_datagrid").datagrid('getPager').data("pagination").options;
            options.pageNumber = 1;
            //重新加载
            $("#single_datagrid").datagrid('options').queryParams = queryParams;
            $("#single_datagrid").datagrid('reload');
        }

        function handleSingleProjectCategaryInfo() {
            var url = "";
            if (flag == 'add') {
                url = '${path}/single/project/categary/insert.do';
            } else if (flag == 'update') {
                url = '${path}/single/project/categary/update.do';
            }
            $.messager.progress();
            $("#edit_form").form("submit",{
                url:url,
                onSubmit:function(){
                    var valid = $(this).form("validate");
                    if (!valid) {
                        $.messager.progress("close");
                    }
                    return valid;
                },
                success:function(data){
                    data=$.parseJSON(data);
                    $.messager.progress("close");
                    if(data.success){
                        $("#edit_dialog").dialog("close");
                        $.messager.show({
                            title: data.msg + "成功",msg: data.msg + "成功!",
                            timeout:600,style:"left:30%;top:10%",
                            showType:"fade"
                        });
                        $('#single_datagrid').datagrid('clearSelections');
                        $("#single_projoct_categary_datagrid").datagrid('clearSelections');
                        $("#single_projoct_categary_datagrid").datagrid("reload");
                    } else {
                        $.messager.alert(data.msg + "失败", data.msg + "失败","error");
                    }
                },
                error:function(data){
                    $.messager.progress("close");
                    $.messager.alert("操作失败", "服务器连接失败!","error");
                }
            });
        }

        function doSearch(searchForm, datagrid) {
            var params = $("#" + searchForm).serializeArray();
            var queryParams = {};
            $.each(params, function() {
                if (queryParams[this.name]) {
                    if (!queryParams[this.name].push) {
                        queryParams[this.name] = [ queryParams[this.name] ];
                    }
                    queryParams[this.name].push(this.value || '');
                } else {
                    queryParams[this.name] = this.value || '';
                }
            });
            if (datagrid == 'single_datagrid') {
                var select = $("#single_projoct_categary_datagrid").datagrid("getSelected");
                if (select != null) {
                    queryParams.singleProjectCategoryId = select.id;
                }
            }
            $("#" + datagrid).datagrid('options').queryParams = queryParams;
            $("#" + datagrid).datagrid('getPager').data("pagination").options.pageNumber = 1;
            $("#" + datagrid).datagrid('clearSelections');
            var select = $('#single_datagrid').datagrid('getSelected');
            if(select != null){
                $('#single_datagrid').datagrid('clearSelections');
            }
            $("#" + datagrid).datagrid('reload');
        }

        function searchReset(searchForm, status, datagrid) {
            $("#" + searchForm).form('clear');
            $("#" + status).combobox('setValue', "");
            var select = $('#' + datagrid).datagrid('getSelected');
            if(select != null){
                $('#' + datagrid).datagrid('clearSelections');
            }
        }

    </script>
    <!--east单项明细列表start-->
    <div data-options="region:'east',split:true,border:false" title="单项列表" style="width:65%;">
        <table id="single_datagrid" class="easyui-datagrid"
               data-options="
                url:'${path}/single/project/selectApply.do',
		   		fit:true,
		   		idField:'id',
		   		rownumbers:true,
		   		toolbar:'#single_tools',
		   		pagination:true,
		   		fitColumns:true,
		   		nowrap:false,
                frozenColumns:[[
                {field:'ck',checkbox:true}]],
		   		singleSelect:false">
            <thead>
            <%--<th data-options="field:'id', align:'center'">ID</th>--%>
            <th data-options="field:'singleProjectCategoryName', align:'center'">单项类名称</th>
            <th data-options="field:'name', align:'center'">单项名称</th>
            <th data-options="field:'price', align:'center'">单项价格(￥)</th>
            <th data-options="field:'des', width:'200px', align:'center'">单项明细</th>
            <th data-options="field:'significance', width:'200px', align:'center'">单项意义</th>
            <th data-options="field:'ord', align:'center'">排序</th>
            <th data-options="field:'status',align:'center',formatter:formatterStatus">是否启用</th>
            </thead>
        </table>
        <div id="single_tools">
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" onclick="$('#single_datagrid').datagrid('reload');">刷新</a>

            <shiro:hasPermission name="/single/project/insert.do">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="openAddApplyBox()">添加</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="/single/project/update.do">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="openUpdateApplyBox()">修改</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="/single/project/delete.do">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="deleteSingleProjectInfo()">删除</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="/single/project/changeSingleStatusToEnableOrDisable.do">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-tip'" onclick="changeProjectStatusToEnableOrDisable()">启用|禁用</a>

            </shiro:hasPermission>

            <form action="#" id="searchSingleForm">
                <input name="name" class="easyui-textbox" data-options="prompt:'请输入单项名称'" style="width:200px"></input>
                <lable>是否启用:</lable>
                <select id="search_single_status" class="easyui-combobox" name="status" data-options="editable:false,panelHeight:'auto'" style="width:100px">
                    <option value="" selected>--请选择--</option>
                    <option value="0">否</option>
                    <option value="1">是</option>
                </select>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="doSearch('searchSingleForm', 'single_datagrid')">查询</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-clear'" onclick="searchReset('searchSingleForm', 'search_single_status', 'single_datagrid')">重置</a>
            </form>
        </div>
    </div>
    <!--end-->

    <!--edit begin-->
    <div class="easyui-dialog" id="edit_apply_dialog" data-options="closed:true,modal:true,width:600,height:400">
        <form id="edit_apply_form" method="post">
            <table align="center" width="100%" style="margin-top: 20px" cellpadding="4">
                <input type="hidden" name="id"/>
                <tr>
                    <td align="right" width="30%"><lable for="name">单项名称:</lable></td>
                    <td width="70%">
                        <input id="edit_apply_name" name="name" class="easyui-validatebox" data-options="required:true" style="width:260px;"/>
                    </td>
                </tr>
                <tr>
                    <td align="right" width="30%"><lable for="des">单项明细:</lable></td>
                    <td width="70%">
                        <textarea id="edit_apply_des" name="des" class="easyui-validatebox" data-options="required:true"></textarea>
                    </td>
                </tr>
                <tr>
                    <td align="right" width="30%"><lable for="significance">单项意义:</lable></td>
                    <td width="70%">
                        <textarea id="edit_apply_significance" name="significance" class="easyui-validatebox" data-options="required:true" style="margin: 0px; height: 78px; width: 235px;"></textarea>
                    </td>
                </tr>
                <tr>
                    <td align="right" width="30%"><lable for="price">单项价格（￥）:</lable></td>
                    <td width="70%">
                        <input id="edit_apply_price" name="price" class="easyui-numberbox" data-options="validType:'number',min:0, precision:2, required:true" style="width:260px;"/>
                    </td>
                </tr>
                <tr>
                    <td align="right" width="30%"><lable for="ord">排序（大前小后）:</lable></td>
                    <td width="70%">
                        <input id="edit_apply_ord" name="ord" class="easyui-numberbox" data-options="required:true,validType:'number'" style="width:260px;"/>
                    </td>
                </tr>
                <%--<tr>--%>
                <%--<td align="right" width="30%"><lable for="status">是否启用:</lable></td>--%>
                <%--<td width="70%">--%>
                <%--<select id="edit_apply_status" class="easyui-combobox" name="status" data-options="required:true,panelHeight:'auto',editable:false" style="width:260px;">--%>
                <%--<option value="0">否</option>--%>
                <%--<option value="1">是</option>--%>
                <%--</select>--%>
                <%--</td>--%>
                <%--</tr>--%>
                <tr>
                    <td align="right" width="30%"><lable for="status">分类:</lable></td>
                    <td width="70%">
                        <select id="edit_category" class="easyui-combobox" name="singleProjectCategoryId" data-options="required:true" style="width:260px;">

                        </select>
                    </td>
                </tr>
            </table>
            <div style="margin-top: 20px;text-align: center">
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'", onclick="handleSingleInfo()">保存</a>
                <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'", onclick="$('#edit_apply_dialog').dialog('close')">取消</a>
            </div>
        </form>
    </div>
    <!--edit end-->

    <div id="editEnableOrDisable" class="easyui-dialog"></div>

    <script>
        var dlg = null;
        $(function () {
            dlg = $('#editEnableOrDisable').dialog({
                title : "启用单项|禁用单项",
                buttons : [ {
                    id : "flagBttOne",
                    text : "启用",
                    handler : function() {
                        projectStatusToEnable();
                    }
                }, {
                    id : "flagBttTwo",
                    text : "禁用",
                    handler : function() {
                        projectStatusToDisable();
                    }
                }, {
                    text : "关闭",
                    handler : function() {
                        dlg.dialog('close');
                    }
                } ]
            });
            dlg.dialog('close');
        })

        function changeProjectStatusToEnableOrDisable() {
            var rowsData = $("#single_datagrid").datagrid("getSelections");//找到所有选中的行
            if (rowsData.length <= 0) {
                $.messager.alert("提示","请选择要操作的单项！","info");
                return;
            }
            dlg.dialog('open'); // 打开窗口
        }

        function projectStatusToEnable() {
            singleProjectStatusHandel(1);
        }

        function projectStatusToDisable() {
            singleProjectStatusHandel(0);
        }

        function singleProjectStatusHandel(status) {
            dlg.dialog('close');
            var rowsData = $("#single_datagrid").datagrid("getSelections");//找到所有选中的行
            var ids = "";
            if (rowsData.length > 0) {
                for (var i = 0; i < rowsData.length; i++) {
                    var row = rowsData[i];
                    if (i == rowsData.length - 1) ids += row.id;
                    else ids += row.id + ',';
                }
            }

            if (ids.length <= 0) {
                $.messager.alert("提示","请选择要操作的单项！","info");
                return;
            }
            var msg = "";
            if (status == 0) {
                msg += "禁用所选单项?";
            } else {
                msg += "启用所选单项? \n" +
                    "注意: 如果单项所属单项类未启用,单项将无法被启用!!!!";
            }
            var url = "${path}/single/project/changeSingleStatusToEnableOrDisable.do";
            $.messager.confirm("确认", "你确定要" + msg, function(yes){
                if(yes){
                    $.messager.progress();
                    $.ajax({
                        url:url,
                        type:"post",
                        data:{ids:ids, status:status},
                        dataType:"json",
                        success:function(data){
                            $.messager.progress("close");
                            if(data.success){
                                $.messager.show({
                                    title:"操作成功",
                                    msg:"操作成功!",
                                    timeout:600,
                                    style:"left:30%;top:10%",
                                    showType:"fade"
                                });
                                $("#single_projoct_categary_datagrid").datagrid("reload");
                                $("#single_datagrid").datagrid("reload");
                            }else{
                                $.messager.alert("操作失败", data.msg,"error");
                            }
                        },
                        error:function(){
                            $.messager.progress("close");
                            $.messager.alert("操作失败", "服务器连接失败!","error");
                        }
                    });
                }
            });
        }


        function handleSingleInfo() {
            var msg = "";
            var url = "";
            if (flag == 'add') {
                url = "${path}/single/project/insert.do";
                msg = "添加";
            } else if(flag == 'update') {
                url = "${path}/single/project/update.do";
                msg = "修改";
            }
            $.messager.progress();
            $("#edit_apply_form").form("submit",{
                url:url,
                onSubmit:function(){
                    var valid = $(this).form("validate");
                    if (!valid) {
                        $.messager.progress("close");
                    }
                    return valid;
                },
                success:function(data){
                    data=$.parseJSON(data);
                    $.messager.progress("close");
                    if(data.success){
                        $("#edit_apply_dialog").dialog("close");
                        $.messager.show({
                            title: msg + "成功",msg: msg + "成功!",
                            timeout:600,style:"left:30%;top:10%",
                            showType:"fade"
                        });
                        $("#single_datagrid").datagrid("reload");
                        $('#single_datagrid').datagrid('clearSelections');
                    }else{
                        $.messager.alert(msg + "失败", data.msg,"error");
                    }
                },
                error:function () {
                    $.messager.progress("close");
                    $.messager.alert("操作失败", "服务器连接失败!","error");
                }
            });
        }

        function openAddApplyBox(){
            flag = 'add';
            $("#edit_apply_form").form("clear");
            handelCategoryCombobox();
            var select = $("#single_projoct_categary_datagrid").datagrid("getSelected");
            if (select != null) {
                $("#edit_category").combobox("setValue", select.id);
            }
            $("#edit_apply_status").combobox('setValue', 1);
            $("#edit_apply_dialog").dialog({title: "添加单项"});
            $("#edit_apply_dialog").dialog("open");
        }

        function openUpdateApplyBox() {
            var rowsData = $("#single_datagrid").datagrid("getSelections");//找到所有选中的行
            if(rowsData.length > 1){
                $.messager.alert("提示","只能修改一项","info");
                return;
            }
            flag = 'update';
            var select = $("#single_datagrid").datagrid("getSelected");
            if(select == null){
                $.messager.alert("提示","请选择要修改的单项！","info");
                return;
            }
            handelCategoryCombobox();
            $("#edit_apply_form").form("load", select);
            $('#edit_apply_dialog').dialog({title: "修改单项"});
            $("#edit_apply_dialog").dialog("open");
        }

        function handelCategoryCombobox() {
            $("#edit_category").combobox({
                url : "${path}/single/project/getSingleProjectCategoryCombobox.do",
                valueField:'id',
                textField:'name',
                panelHeight : 'auto',
                editable:false
            });
        }

        function deleteSingleProjectInfo() {
            var rowsData = $("#single_datagrid").datagrid("getSelections");//找到所有选中的行
            if(rowsData.length > 1){
                $.messager.alert("提示","只能删除一项","info");
                return;
            }
            lananDelete("single_datagrid", "确定要删除该单项吗?", "${path}/single/project/delete.do");
        }

        function formatterStatus(status) {
            if (status == 0) return '<span style="color:red">' + '禁用' + '</span>';
            else if (status == 1) return '<span style="color:green">' + '启用' + '</span>';
        }


        function changeSingleStatusToEnableOrDisable() {
            var select = $("#single_projoct_categary_datagrid").datagrid("getSelected");
            if(select == null){
                $.messager.alert("提示","请选择要操作的单项分类！","info");
                return;
            }
            var msg = "";
            var status;
            var url = "";
            if (select.status == 0) {
                msg = "启用";
                status = 1;
            } else {
                msg = "禁用";
                status = 0;
            }
            if (status == 0) msg += "该单项分类？如果禁用该分类,其下的所有单项将被禁用!";
            else msg += "该单项分类？"
            url = "${path}/single/project/changeCategoryStatusToEnableOrDisable.do";
            $.messager.confirm("确认", "你确定要" + msg, function(yes){
                if(yes){
                    $.messager.progress();
                    $.ajax({
                        url:url,
                        type:"post",
                        data:{id:select.id, status:status},
                        dataType:"json",
                        success:function(data){
                            $.messager.progress("close");
                            if(data.success){
                                $.messager.show({
                                    title:"操作成功",
                                    msg:"操作成功!",
                                    timeout:600,
                                    style:"left:30%;top:10%",
                                    showType:"fade"
                                });
                                $("#single_projoct_categary_datagrid").datagrid("reload");
                                $("#single_datagrid").datagrid("reload");
                            }else{
                                $.messager.alert("操作失败", data.msg,"error");
                            }
                        },
                        error:function(){
                            $.messager.progress("close");
                            $.messager.alert("操作失败", "服务器连接失败!","error");
                        }
                    });
                }
            });
        }


    </script>
</div>
</body>
</html>
